/**
 * Set is important News
 * 
 * @param int newsID
 * @param String url to call
 * @return void
 */
function setIsImportant(newsID, url) {
	setFlag(newsID, 1, url, function(data) {
		if (data.error) {
            jQuery.facebox(data.message);
        } else {
        	$('#isImportant-'+newsID).show();
            $('#isNotImportant-'+newsID).hide();
        }
	});
}

/**
 * Set is not important News
 * 
 * @param int newsID
 * @param String url to call
 * @return void
 */
function setIsNotImportant(newsID, url) {
	setFlag(newsID, 0, url, function(data) {
		if (data.error) {
            jQuery.facebox(data.message);
        } else {
        	$('#isImportant-'+newsID).hide();
        	$('#isNotImportant-'+newsID).show();
        	
        	if($('.selectedFT').attr('id') == 'importantNewsLink') {
        		$('#News-'+newsID).remove();
        	}
        }
	});
}


/**
 * Set is Read News
 * 
 * @param int newsID
 * @param String url to call
 * @return void
 */
function setIsRead(newsID, url) {
	setFlag(newsID, 1, url, function(data) {
		if (data.error) {
            jQuery.facebox(data.message);
        } else {
        	if ($('#News-'+newsID).hasClass('isNotReadNews')) {
	            var text = $('span.selectedCounter').html().match(/\d+/);
	       	 	text = parseInt(text) - 1;
	       	    $('span.selectedCounter').empty();
	       	    $('span.selectedCounter').html("("+ text +")");
        	}
        	
            $('#SetIsRead-'+newsID).show();
            $('#News-'+newsID).removeClass('isNotReadNews');
            
            $(".ulNewsList:not(.isNotReadNews)").bind('mouseenter', mouseenter);
       	 	$(".ulNewsList:not(.isNotReadNews)").bind('mouseleave', mouseleave);
        }
	});
}

/**
 * Set is not Read News
 * 
 * @param int newsID
 * @param String url to call
 * @return void
 */
function setIsNotRead(newsID, url) {
	setFlag(newsID, 0, url, function(data) {
		if (data.error) {
            jQuery.facebox(data.message);
        } else {
        	$('#SetIsRead-'+newsID).hide();
        	$('#News-'+newsID).addClass('isNotReadNews');
        	$('#News-'+newsID).unbind('mouseenter', mouseenter);
            $('#News-'+newsID).unbind('mouseleave', mouseleave);
        	
            var text = $('span.selectedCounter').html().match(/\d+/);
       	 	text = parseInt(text) + 1;
       	 	$('span.selectedCounter').empty();
       	    $('span.selectedCounter').html("("+ text +")");
        }
	});
}

/**
 * 
 * 
 * @param feedID
 * @param url
 * @param callback
 * @return
 */
function loadNews(feedID, url, callback) {
	jQuery.ajax({
        type: "POST",
        url: url,
        data: {feedID: feedID},
        success: function(data) {
            // Execute the callback passing it the result
        	callback(data);
        },
        error: function(xhr, textStatus, errorThrown) {
            //TODO Process errors with a specifc callback
        	jQuery.facebox(xhr.responseText);
        }
    });
}

/**
 * Delete a User Feed, Category or News
 * 
 * @param int itemID to delete
 * @param String url to call
 * @param jQuery delBox object to remove from dom
 * @param messages an array object containig messages for dialogbox 
 * @return void
 */
function deleteUserElement(itemID, url, delBox, messages) {
	$('#confirmbox').dialog('option','buttons', {
        "OK": function() {
            $(this).dialog('close');
			executeRequest(itemID, url, function(data) {
				if (data.error) {
					jQuery.facebox(data.message);
				} else {
					delBox.remove();
					if (delBox.hasClass('ulNewsList') && delBox.hasClass('isNotReadNews')) {
			            var text = $('span.selectedCounter').html().match(/\d+/);
			       	 	text = parseInt(text) - 1;
			       	    $('span.selectedCounter').empty();
			       	    $('span.selectedCounter').html("("+ text +")");
		        	}
				}
			});
		},
	    "NO": function() {
	        $(this).dialog('close');
	    }
	});
	
	showConfirmDialog($('#confirmbox'), messages);
}

/**
 * 
 * 
 * @param String url to call
 * @param data object array of params to send
 * @param String callback function to signal completed task
 * @return void
 */
function loadViewAddUserFeed(url, data, callback) {
    jQuery.ajax({
        type: "POST",
        url: url,
        data: data,
        success: function(data) {
            if (callback != null) {
            	callback(data);
            }
        },
        error: function(xhr, textStatus, errorThrown) {
           
        }
    });
}
